/**
 * Title :	Property view
 * Description: Property editor
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package bsys.propertyview;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;

/**
 * Change notifier
 * 
 * @author Bernard Bou
 */
public class ChangeNotifier
{
	/**
	 * Listener list
	 */
	protected EventListenerList theListeners = new EventListenerList();

	/**
	 * Constructor
	 */
	public ChangeNotifier()
	{
	}

	/**
	 * Add a listener
	 * 
	 * @param thisListener
	 *        listener
	 */
	public void addChangeListener(ChangeListener thisListener)
	{
		theListeners.add(ChangeListener.class, thisListener);
	}

	/**
	 * Remove listener
	 * 
	 * @param thisListener
	 *        listener
	 */
	public void removeChangeListener(ChangeListener thisListener)
	{
		theListeners.remove(ChangeListener.class, thisListener);
	}

	/**
	 * Get change listeners
	 * 
	 * @return list of listeners
	 */
	public ChangeListener[] getChangeListeners()
	{
		return (ChangeListener[]) (theListeners.getListeners(ChangeListener.class));
	}

	/**
	 * Notifies all listeners that have registered interest for notification on this event type.
	 * 
	 * @see EventListenerList
	 */
	public void fireStateChanged(ChangeEvent changeEvent)
	{
		// guaranteed to return a non-null array
		Object[] theseListeners = theListeners.getListenerList();

		// process the listeners last to first, notifying those that are interested in this event
		for (int i = theseListeners.length - 2; i >= 0; i -= 2)
		{
			if (theseListeners[i] == ChangeListener.class)
			{
				// fire
				((ChangeListener) theseListeners[i + 1]).stateChanged(changeEvent);
			}
		}
	}
}
